package com.appkubes.mall.admin.application.user.account.account.service.impl;

import com.appkubes.common.exception.BusinessException;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.admin.application.user.account.account.dao.UserAccountDao;
import com.appkubes.mall.admin.application.user.account.account.entity.dto.AccountResultDTO;
import com.appkubes.mall.admin.application.user.account.account.entity.dto.AccountStatisDTO;
import com.appkubes.mall.admin.application.user.account.account.entity.vo.AccountSearchVO;
import com.appkubes.mall.admin.application.user.account.account.service.UserAccountService;
import com.appkubes.mall.admin.application.user.account.history.service.UserAccountHistoryService;
import com.appkubes.mall.core.entity.MallUserAccount;
import com.appkubes.mall.core.service.impl.BaseMallUserAccountServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: Rongjie
 * @Date: 2020/2/29
 * @Description:
 */
@Service
@Slf4j
public class UserAccountServiceImpl extends BaseMallUserAccountServiceImpl implements UserAccountService {

    @Resource
    UserAccountDao userAccountDao;

    /**
     * 确认订单
     *
     * @param userKey
     * @param orderKey
     * @return
     * @throws BusinessException
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public Boolean orderConfirm(String userKey, String orderKey) throws BusinessException {
//        MallUserAccountHistory accountHistory = userAccountHistoryService.getAccountOrderHistory(orderKey);
//        log.info("orderConfirm accountHistory: {}", accountHistory);
//        if (accountHistory != null && MallUserAccountHistoryConstants.State.PENDING.name().equalsIgnoreCase(accountHistory.getState())) {
//            log.info("未发放，则进行发放操作:{}", accountHistory.getAccountSourceKey());
//            //未发放，则进行发放操作
//            //添加佣金
//            MallUserAccount userAccount = getUserAccount(accountHistory.getUserKey()); //给用户确认佣金
//            userAccount.setTotalBrokerage(userAccount.getTotalBrokerage().add(accountHistory.getBrokerage()));
//            userAccount.setUpdateTime(DateUtils.getCurrentTimestamp());
//            Boolean success = this.updateById(userAccount);
//            if (!success) {
//                throw new BusinessException("发放佣金错误");
//            }
//            accountHistory.setState(MallUserAccountHistoryConstants.State.CONFIRM.name());
//            success = userAccountHistoryService.updateById(accountHistory);
//            if (!success) {
//                throw new BusinessException("更新账户状态错误");
//            }
//        }
//        log.info("orderConfirm: userKey:{}, orderKey:{}", userKey, orderKey);
        return true;
    }

    @Override
    public IPage<AccountResultDTO> search(AccountSearchVO accountSearchVO) {
        IPage<AccountResultDTO> page = new Page<>(accountSearchVO.getPage(), accountSearchVO.getSize());
        return userAccountDao.search(page, accountSearchVO);
    }

    @Override
    public AccountStatisDTO statis(AccountSearchVO accountSearchVO) {
        return userAccountDao.statis(accountSearchVO);
    }


    MallUserAccount getUserAccount(String userKey) {
        MallUserAccount userAccount = this.getById(userKey);
        if (userAccount == null) {
            userAccount = new MallUserAccount();
            userAccount.setUserKey(userKey); //创建账户
            userAccount.setTotalBrokerage(BigDecimal.valueOf(0));
            userAccount.setCreateTime(DateUtils.DateToTimestamp(new Date()));
            userAccount.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
            userAccount.setAppKey(null);
            this.save(userAccount);
        }
        return userAccount;
    }
}
